import { defineStore } from 'pinia';
import pics from '@/api/pics.js';

export const usePicStore = defineStore('pic', {
  state: () => ({
    pickerVisable: false,
    defaultDate: ['2019-06-01', '2024-12-01'],
    imgList: [],
    imgurl: [],
    displayMode: true,
    enlargedImage: null,
    deletingFileName: null,
    enlargedImageUrl: null,
    page: 1,
    limit: 6,
    status: 'more',
  }),
  actions: {
    openPicker() {
      this.pickerVisable = true;
    },
    confirmDate(date) {
      this.imgList = [];
      const arry = date.toString().split(',');
      const data = {
        startTime: arry[0] + ' 00:00:00',
        endTime: arry[1] + ' 23:59:59',
      };
      this.searchDataunit(data);
      this.pickerVisable = false;
    },
    doSearch(confirm) {
      if (confirm.detail.value === '') {
        return;
      }
      this.searchDataunit({ userName: confirm.detail.value });
    },
    // 直接定义计算属性，不需要使用 computed 函数包裹
    get icoUrl() {
      return this.displayMode? '/static/img/ico/v.png' : '/static/img/ico/c.png';
    },
    fetchImageList() {
      this.page = 1;
      this.imgurl = [];
      this.status = 'more';
      const data = {
        userName: uni.getStorageSync('userName'),
        page: this.page,
        limit: this.limit,
      };
      this.searchDataunit(data);
    },
    async searchDataunit(data = {}) {
      const response = await pics.fetchImageList(data);
      uni.stopPullDownRefresh();
      this.imgList.splice(0, this.imgList.length,...response.data);
      for (let index in response.data) {
        this.imgurl.push(response.data[index].imgUrl);
      }
    },
    toggleImageSize(index) {
      // 预览照片
      uni.previewImage({
        urls: this.imgurl,
        current: index,
        longPressActions: {
          itemList: ['发送给朋友', '保存照片', '收藏', '删除'],
          success: function (data) {
            console.log(data);
          },
          fail: function (err) {
            console.log(err.errMsg);
          },
        },
      });
    },
    showDeleteConfirm(fileName) {
      this.deletingFileName = fileName;
      this.showDeletePopup = true;
    },
    hideDeletePopup() {
      this.showDeletePopup = false;
    },
    async confirmDelete() {
      const imgToDelete = this.imgList.find(img => img.FileName === this.deletingFileName);
      await pics.deleteImg({ fileName: imgToDelete.fileName });
      this.imgList = this.imgList.filter(img => img.fileName!== this.deletingFileName);
      this.hideDeletePopup();
    },
    addList() {
      this.status = 'loading';
      this.page++;
      this.imgurl = [];
      const data = {
        userName: uni.getStorageSync('userName'),
        page: this.page,
        limit: this.limit,
      };
      pics.fetchImageList(data).then(res => {
        if (res.data.length!== 0) {
          this.imgList = this.imgList.concat(res.data);
          this.status = 'more';
          for (let index in this.imgList) {
            this.imgurl.push(this.imgList[index].imgUrl);
          }
        } else {
          this.status = 'noMore';
        }
      }).catch(err => {
        console.log(err);
        uni.showToast({
          title: '出现错误，请联系管理员',
          icon: 'error',
          duration: 2000,
        });
      });
    },
  },
});